﻿Public Class searchTasksForm
    Public idSelected As Integer

    Private Sub searchTasksForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GenericAdministrator.loadGrid(Me.dgvSearchTasks, Me.bdnSearchTasks, TaskAdministrator.search())

        Dim stateScope As StateScope = New StateScope(stateScope.STATE_SCOPE_TASK)
        Dim state As State = New State()
        state.stateScopeProperty = stateScope

        GenericAdministrator.loadComboBox(Me.cboState, StateAdministrator.searchDataByFilter(state), "id", "name")
        GenericAdministrator.loadComboBox(Me.cboTaskType, GenericAdministrator.searchDataByFilter(New TaskType()), "id", "name")
        GenericAdministrator.loadComboBox(Me.cboResource, ResourceAdministrator.search("id, (surname || ', ' || firstname) as name"), "id", "name")
        Me.setDataGridViewHeaders()
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Dim state As New State
        If chkAllStates.Checked Then
            state.idProperty = Me.cboState.SelectedValue
        End If

        Dim resource As New Resource(Me.cboResource.SelectedValue)
        Dim projectMember As New ProjectMember()
        projectMember.resourceProperty = resource

        Dim taskType As New TaskType
        taskType.idProperty = Me.cboTaskType.SelectedValue

        Dim task As New Task(Nothing, Me.txtName.Text, Nothing, Nothing, Nothing, Nothing, Nothing, taskType, Nothing, state, projectMember)
        GenericAdministrator.loadGrid(Me.dgvSearchTasks, Me.bdnSearchTasks, TaskAdministrator.searchDataByFilterToSearchForm(task))
    End Sub

    Private Sub bdnEditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bdnEditButton.Click
        idSelected = Me.dgvSearchTasks.CurrentRow.Cells(0).Value
        Dim task As Task = TaskAdministrator.searchDataById(idSelected)
        Me.Close()
        Dim taskForm As New TaskForm(task)
        taskForm.Show()
    End Sub

    Private Sub setDataGridViewHeaders()
        Me.dgvSearchTasks.Columns(0).HeaderText = "Id"
        Me.dgvSearchTasks.Columns(0).Visible = False
        Me.dgvSearchTasks.Columns(1).HeaderText = "Nombre"
        Me.dgvSearchTasks.Columns(2).HeaderText = "Tipo de Tarea"
        Me.dgvSearchTasks.Columns(3).HeaderText = "Recurso"
        Me.dgvSearchTasks.Columns(4).HeaderText = "Fecha Estimada de Inicio"
        Me.dgvSearchTasks.Columns(5).HeaderText = "Fecha Estimada de Fin"
        Me.dgvSearchTasks.Columns(6).HeaderText = "Actividad"
        Me.dgvSearchTasks.Columns(7).HeaderText = "Proyecto"
        Me.dgvSearchTasks.Columns(8).HeaderText = "Estado"
    End Sub
End Class